package com.travel.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

public class SteamUtil {
    //    图片流转换
    public static byte[] readSteamImg(String pathImg){
        InputStream stream = null;
        byte[] bytes = null;
        try {
            stream = new FileInputStream(pathImg);
            bytes = new byte[stream.available()];
            stream.read(bytes);
            stream.close();
        } catch (IOException e) {
            throw new RuntimeException(e+"图片转换异常");
        }
        return bytes;
    }
//    从数据库读出图片并且转换为base64编码格式并且在页面上展示
    public static String writeSteamImg(byte[] img){
        byte[] bytes = Base64.getEncoder().encode(img);
        String s = new String(bytes);
        return s;
    }
    public static String convertRemoteVideoToBase64(String videoUrl) {
        try {
            URL url = new URL(videoUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            try (InputStream inputStream = connection.getInputStream()) {
                byte[] videoBytes = inputStream.readAllBytes();
                return Base64.getEncoder().encodeToString(videoBytes);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
